<template>
  <div class="banner">
    <el-col :span="24" class="action-bar">
      <el-button type="primary" size="medium" @click="addBtnReset" style="margin: 0 20px;">新增</el-button>
    </el-col>

    <!--列表-->
    <el-table :data="levelList" highlight-current-row class="el-table-style">
      <el-table-column type="index" min-width="60">
      </el-table-column>
      <el-table-column prop="name" label="名称" min-width="100"></el-table-column>
      <el-table-column prop="level" label="排序" min-width="100">
      </el-table-column>
      <el-table-column prop="created_at" label="创建时间" min-width="100">
      </el-table-column>
      <el-table-column label="操作" min-width="150">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="getUnitGradeDetail(scope.row.id)">编辑</el-button>
          <el-button type="danger" class="danger-btn" size="mini" @click="delUnitGrade(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-col :span="24" class="footer-bar">
      <!-- <div class="batch"><el-button size="medium ">批量删除</el-button></div> -->
      <div class="page-code">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="pageTotal"
          :page-size="limit"
          @current-change="getUnitGradeList"
        >
        </el-pagination>
      </div>
    </el-col>

    <!-- 新增 -->
    <el-dialog title="新增" :visible.sync="addFormVisible" width="40%">
      <el-form label-width="80px">
        <el-form-item label="社区名称">
          <el-input v-model="addForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序">
          <el-input placeholder="数字越小,排序越靠前" v-model="addForm.level" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addCancelBtn">取消</el-button>
        <el-button type="primary" @click="addSubmitBtn">提交</el-button>
      </div>
    </el-dialog>

    <!-- 编辑 -->
    <el-dialog title="编辑" :visible.sync="editFormVisible" width="40%">
      <el-form label-width="80px">
        <el-form-item label="社区名称">
          <el-input v-model="setForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序">
          <el-input  placeholder="数字越小,排序越靠前" v-model="setForm.level" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editCancelBtn">取消</el-button>
        <el-button type="primary" @click="editSubmitBtn">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    export default {
      name: "vipCompany_level",
      data(){
          return{
            addFormVisible:false, //新建界面显影属性
            editFormVisible: false,//编辑界面显影属性
            levelList:[],

            addForm:{
              name: '',
              level: ''
            }, //添加表单

            setForm:{
              id: '',
              name: '',
              level: ''
            },//编辑表单
            pageTotal: 0,
            limit: 10
          }
      },
      mounted(){
        this.getUnitGradeList();
      },
      methods:{
        //获取单位等级列表
        async getUnitGradeList(page){
          let params = {
            page: page || 1,
            limit: this.limit
          }
          const res = await this.$api.getUnitGradeList(params);
          if (res.data.level == 'success'){
            let list = res.data.data;
            this.levelList = list;
            this.pageTotal = res.data.page_info.total
          } else{
            this.$message({
              message: res.data.message,
              type: 'warning'
            });
          }
        },
        //获取单位等级详情
        async getUnitGradeDetail(id){
          const res = await this.$api.getUnitGradeDetail({id:id});
          if (res.data.level == 'success'){
            this.setForm.id = res.data.data.id;
            this.setForm.name = res.data.data.name;
            this.setForm.level = res.data.data.level;
            this.editFormVisible = true;
          } else{
            this.$message({
              message: res.data.message,
              type: 'warning'
            });
          }
        },
        //新增按钮重置表单
        addBtnReset(){
          this.addForm.name = "";
          this.addForm.level = '';
          this.addFormVisible = true;
        },
        //新增提交按钮
        async addSubmitBtn(){
          const res = await this.$api.setUnitGrade(this.addForm);
          if (res.data.level == 'success'){
            this.$message({
              message: res.data.message,
              type: 'success'
            });
            this.addCancelBtn();
            this.getUnitGradeList();
          } else {
            this.$message({
              message: res.data.message,
              type: 'warning'
            });
          }
        },
        //新增取消按钮
        addCancelBtn(){
          this.addForm.name = "";
          this.addForm.level = '';
          this.addFormVisible = false;
        },
        //编辑提交按钮
        async editSubmitBtn(){
          const res = await this.$api.setUnitGrade(this.setForm);
          if (res.data.level == 'success'){
            this.$message({
              message: res.data.message,
              type: 'success'
            });
            this.editCancelBtn()
            this.getUnitGradeList();
          } else {
            this.$message({
              message: res.data.message,
              type: 'warning'
            });
          }
        },
        //编辑取消按钮
        editCancelBtn(){
          this.setForm.id = "";
          this.setForm.name = "";
          this.setForm.level = "";
          this.editFormVisible = false;
        },
        //删除单位等级
        async delUnitGrade(id){
          const res = await this.$api.delUnitGrade({id:id});
          if (res.data.level == 'success'){
            this.$message({
              message: res.data.message,
              type: 'success'
            });
            this.getUnitGradeList();
          } else {
            this.$message({
              message: res.data.message,
              type: 'warning'
            });
          }
        },
      }
    }
</script>

<style lang='scss' scoped>
  .banner{
    .action-bar{
      background-color: #f2f2f2;
      height: 60px;
      margin: 20px 0px;
      box-sizing: border-box;
      display: flex;
      align-items: center;
      padding-left: 20px;
      .action-bar-input{
        width: 250px;
        margin-right: 14px;
      }
    }
    .el-table-style{
      width: 100%;
      border: 1px solid #f2f2f2;
    }
    .footer-bar{
      display: flex;
      justify-content: space-between;
      align-items: center;
      background: #f2f2f2;
      padding: 8px;
      margin: 10px 0px;
      box-sizing: border-box;
    }
    .dialog-footer{
      text-align: center;
    }
  }
</style>
<style>
  .el-pagination.is-background .el-pager li,
  .el-pagination.is-background .btn-prev,
  .el-pagination.is-background .btn-next
  {
    background-color: #fff;
    border: 1px solid #f1f1f1;
    margin: 0;
  }
  .member .el-dialog__body{overflow: auto;}
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
